﻿using System;
using UnityEngine;

namespace CGF
{
    /// <summary>
    /// 播放短音效专用
    /// </summary>
    public static class SoundPlayer
    {
        private static AudioSource _audioSource;
        private static AudioSource AudioSource
        {
            get
            {
                if (!_audioSource)
                {
                    GameObject go = new GameObject(nameof(SoundPlayer));
                    GameObject.DontDestroyOnLoad(go);
                    _audioSource = go.AddComponent<AudioSource>();
                    _audioSource.playOnAwake = false;
                    _audioSource.loop = false;
                    _audioSource.enabled = IsEnable;
                }
                return _audioSource;
            }
        }
        private static bool _isEnable, _isEnableReady;

        /// <summary>
        /// 全局音效音量开关
        /// </summary>
        public static bool IsEnable
        {
            get
            {
                if (!_isEnableReady)
                {
                    _isEnableReady = true;
                    _isEnable = PlayerPrefs.GetInt("SoundIsEnable", 1) == 1;
                }
                return _isEnable;
            }
            set
            {
                _isEnable = value;
                AudioSource.enabled = value;
                PlayerPrefs.SetInt("SoundIsEnable", value ? 1 : 0);
            }
        }

        /// <summary>
        /// 全局音效音量
        /// </summary>
        public static float Volume
        {
            get { return PlayerPrefs.GetFloat("SoundVolume", 1); }
            set
            {
                AudioSource.volume = value;
                PlayerPrefs.SetFloat("SoundVolume", value);
            }
        }

        /// <summary>
        /// 播放一个短音效
        /// </summary>
        public static void Play(AudioClip clip, float volume = 1)
        {
            Play(new AudioClip[] { clip }, volume);
        }

        /// <summary>
        /// 按顺序播放多个短音效
        /// </summary>
        public static void Play(AudioClip[] clips, float volume = 1)
        {
            if (!IsEnable) return;

            if (clips.Length == 1)
            {
                if (!clips[0])
                {
                    Debug.Log("audioClip is null");
                }
                AudioSource.PlayOneShot(clips[0], Volume * volume);
            }
            else if (clips.Length > 1)
            {
                PlayMulti(clips, volume);
            }
        }

        private static async void PlayMulti(AudioClip[] clips, float volume)
        {
            for (int i = 0; i < clips.Length; i++)
            {
                if (!clips[i])
                {
                    Debug.Log("audioClip is null");
                }
                AudioSource.PlayOneShot(clips[i], Volume * volume);
                await CTask.Wait(clips[i].length);
            }
        }

        internal static void Play(object chip_get)
        {
            throw new NotImplementedException();
        }
    }
}
